package com.atguigu.flink05;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.WaterSensorMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

/**
 * @author Felix
 * @date 2024/2/23
 * 该案例演示了窗口增量聚合
 *      reduce方法中第一个参数 规约的结果  第二个参数：新来的数据
 *      当窗口中只有一条数据的时候，reduce方法不会被执行
 *      之后，每来一条数据，reduce方法都会被执行一次
 *      当处理时间到了窗口的最大时间了，会触发窗口的计算  ---触发计算后才会输出整个窗口聚合的结果
 *
 *      窗口的最大时间 = 窗口结束时间 - 1ms
 *
 * reduce增量聚合有一个限制：窗口中的元素类型、聚合类型、向下游传递的数据类型必须要一致
 */
public class Flink04_window_reduce {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //TODO 2.从指定端口读取数据
        DataStreamSource<String> lineStrDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.对读取数据进行类型转换    String - WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = lineStrDS.map(new WaterSensorMapFunction());
        //TODO 4.按照传感器id进行分组
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);
        //TODO 5.开窗   滚动处理时间窗口  窗口大小10s
        WindowedStream<WaterSensor, String, TimeWindow> windowDS
                = keyedDS.window(TumblingProcessingTimeWindows.of(Time.seconds(10)));
        //TODO 6.对窗口数据进行规约聚合计算  --- 增量聚合 reduce
        // reduce  aggregate apply  process
        SingleOutputStreamOperator<WaterSensor> reduceDS = windowDS.reduce(
                new ReduceFunction<WaterSensor>() {
                    @Override
                    public WaterSensor reduce(WaterSensor value1, WaterSensor value2) throws Exception {
                        System.out.println("规约的结果:" + value1);
                        System.out.println("新来的数据:" + value2);

                        return new WaterSensor(value1.id, System.currentTimeMillis(), value1.vc + value2.vc);
                    }
                }
        );
        //TODO 7.将聚合结果进行输出
        reduceDS.print();
        //TODO 8.提交作业
        env.execute();

    }
}
